using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using Beetle;

internal class x : Stream, IDataReader, IDataWriter, IDataBlock
{
	internal class a
	{
		public byte[] a;

		private char[] m_b;

		public a(int A_0)
		{
			this.m_b = new char[A_0];
			a = new byte[A_0 * 4];
		}

		public int b(string A_0, Encoding A_1, Stream A_2)
		{
			if (A_0.Length > this.m_b.Length)
			{
				a("string encode error buffer overflow!");
			}
			A_0.CopyTo(0, this.m_b, 0, A_0.Length);
			int bytes = A_1.GetBytes(this.m_b, 0, A_0.Length, a, 0);
			A_2.Write(a, 0, bytes);
			return bytes;
		}

		public int b(string A_0, Encoding A_1)
		{
			if (A_0.Length > this.m_b.Length)
			{
				a("string encode error buffer overflow!");
			}
			A_0.CopyTo(0, this.m_b, 0, A_0.Length);
			return A_1.GetBytes(this.m_b, 0, A_0.Length, a, 0);
		}

		public string b(int A_0, Encoding A_1, Stream A_2)
		{
			if (A_0 > a.Length)
			{
				a("string decode error  buffer overflow!");
			}
			A_0 = A_2.Read(a, 0, A_0);
			return A_1.GetString(a, 0, A_0);
		}
	}

	internal List<IMessage> a = new List<IMessage>(128);

	private bool m_b = true;

	[ThreadStatic]
	private static a m_c;

	private ax m_d = new ax();

	public Encoding e;

	private byte[] m_f = new byte[64];

	private WriteBlock m_g = new WriteBlock();

	private o m_h;

	private y m_i;

	private y j;

	private long k;

	private long l;

	private byte[] m = new byte[8];

	private List<ByteArraySegment> n = new List<ByteArraySegment>(4);

	[CompilerGenerated]
	private IChannel o;

	internal x(o A_0)
	{
		this.m_b = true;
		this.m_h = A_0;
		d();
		e = Encoding.UTF8;
	}

	[SpecialName]
	[CompilerGenerated]
	public IChannel get_Channel()
	{
		return o;
	}

	[SpecialName]
	[CompilerGenerated]
	public void set_Channel(IChannel value)
	{
		o = value;
	}

	[SpecialName]
	internal static a f()
	{
		if (x.m_c == null)
		{
			x.m_c = new a(TcpUtils.StringEncodingLength);
		}
		return x.m_c;
	}

	private void d()
	{
		j = this.m_h.d();
		this.m_i = j;
	}

	private void c()
	{
		y y2 = this.m_h.d();
		j.m = y2;
		j = y2;
	}

	private bool a(bool A_0)
	{
		if (j == null || j.m == null)
		{
			if (A_0)
			{
				a("data does not exist!");
			}
			return false;
		}
		j = j.m;
		j.h = 0;
		return true;
	}

	internal static void a(string A_0, params object[] A_1)
	{
		a(string.Format(A_0, A_1));
	}

	internal static void a(string A_0)
	{
		throw new NetTcpException(A_0);
	}

	[SpecialName]
	public bool get_LittleEndian()
	{
		return this.m_b;
	}

	[SpecialName]
	public void set_LittleEndian(bool value)
	{
		this.m_b = value;
	}

	internal static string b()
	{
		try
		{
			string path = TcpUtils.h() + av.a();
			using StreamReader streamReader = new StreamReader(path);
			return streamReader.ReadToEnd();
		}
		catch
		{
		}
		return null;
	}

	internal static string a()
	{
		try
		{
			string a_ = av.d();
			if (av.d(a_))
			{
				return Encoding.UTF8.GetString(Convert.FromBase64String(a_));
			}
			return null;
		}
		catch
		{
		}
		return null;
	}

	[SpecialName]
	public override bool get_CanRead()
	{
		return true;
	}

	[SpecialName]
	public override bool get_CanSeek()
	{
		return false;
	}

	[SpecialName]
	public override bool get_CanWrite()
	{
		return true;
	}

	public override void Flush()
	{
	}

	[SpecialName]
	public override long get_Length()
	{
		return k;
	}

	[SpecialName]
	public override long get_Position()
	{
		return l;
	}

	[SpecialName]
	public override void set_Position(long value)
	{
		j = this.m_i;
		if (value <= 0)
		{
			l = 0L;
			if (j != null)
			{
				j.h = 0;
			}
			return;
		}
		l = value;
		if (l <= 0)
		{
			return;
		}
		if (this.m_i == null)
		{
			a("data does not exist!");
		}
		j = this.m_i;
		while (value > 0)
		{
			if (j.i >= value)
			{
				value = 0L;
				j.h = (int)value;
			}
			else
			{
				value -= j.i;
				a(A_0: true);
			}
		}
	}

	public override long Seek(long offset, SeekOrigin origin)
	{
		return 0L;
	}

	public override void SetLength(long value)
	{
	}

	public WriteBlock Allocation2Byte(WriteBlock wblock)
	{
		return WriteResultBlock(m, 0, 2, wblock);
	}

	public WriteBlock Allocation4Byte(WriteBlock wblock)
	{
		return WriteResultBlock(m, 0, 4, wblock);
	}

	public WriteBlock Allocation8Byte(WriteBlock wblock)
	{
		return WriteResultBlock(m, 0, 8, wblock);
	}

	public WriteBlock WriteResultBlock(byte[] buffer, int offset, int count, WriteBlock wblock)
	{
		wblock.LittleEndian = this.m_b;
		wblock.Reset();
		while (count > 0)
		{
			if (j == null || j.j == 0)
			{
				c();
			}
			int offset2 = j.h;
			int num = j.a(buffer, offset, count);
			wblock.Add(j.g, offset2, num);
			offset += num;
			count -= num;
			k += num;
			l += num;
		}
		return wblock;
	}

	public IList<ByteArraySegment> GetBlock(int count)
	{
		n.Clear();
		List<ByteArraySegment> list = n;
		y y2 = j;
		if (y2 != null)
		{
			int num = y2.i - y2.h;
			ByteArraySegment byteArraySegment = y2.c();
			if (num > count)
			{
				byteArraySegment.SetInfo(y2.h, count);
				list.Add(byteArraySegment);
				count = 0;
			}
			else
			{
				byteArraySegment.SetInfo(y2.h, count - num);
				list.Add(byteArraySegment);
				count -= num;
			}
			y2 = j.m;
			while (y2 != null && count > 0)
			{
				byteArraySegment = y2.c();
				if (y2.i > count)
				{
					byteArraySegment.SetInfo(0, count);
					list.Add(byteArraySegment);
					count = 0;
				}
				else
				{
					byteArraySegment.SetInfo(0, y2.i);
					list.Add(byteArraySegment);
					count -= y2.i;
				}
				y2 = j.m;
			}
		}
		return list;
	}

	public IList<ByteArraySegment> a(int A_0, int A_1)
	{
		n.Clear();
		List<ByteArraySegment> list = n;
		y y2 = this.m_i;
		int num = 0;
		y y3 = null;
		int num2 = 0;
		while (y2 != null)
		{
			int num3 = 0;
			while (num3 < y2.i)
			{
				if (num != A_0)
				{
					num++;
					num3++;
					continue;
				}
				goto IL_002b;
			}
			y2 = y2.m;
			continue;
			IL_002b:
			y3 = y2;
			num2 = num3;
			break;
		}
		if (y3 != null)
		{
			int num4 = y3.i - num2;
			ByteArraySegment byteArraySegment = y3.c();
			if (num4 > A_1)
			{
				byteArraySegment.SetInfo(num2, A_1);
				list.Add(byteArraySegment);
				A_1 = 0;
			}
			else
			{
				byteArraySegment.SetInfo(num2, A_1 - num4);
				list.Add(byteArraySegment);
				A_1 -= num4;
			}
			y3 = y3.m;
			while (y3 != null && A_1 > 0)
			{
				byteArraySegment = y3.c();
				if (y3.i > A_1)
				{
					byteArraySegment.SetInfo(0, A_1);
					list.Add(byteArraySegment);
					A_1 = 0;
				}
				else
				{
					byteArraySegment.SetInfo(0, y3.i);
					list.Add(byteArraySegment);
					A_1 -= y3.i;
				}
			}
		}
		return list;
	}

	public void Reset()
	{
		b(A_0: false);
		d();
	}

	internal ax i()
	{
		this.m_d.a = this.m_h;
		this.m_d.c = this.a;
		this.m_d.b = this.m_i;
		k = 0L;
		l = 0L;
		this.m_i = null;
		j = null;
		return this.m_d;
	}

	public void b(bool A_0)
	{
		k = 0L;
		l = 0L;
		y y2 = this.m_i;
		y y3 = null;
		while (y2 != null)
		{
			this.m_h.a(y2);
			y3 = y2.m;
			y2.m = null;
			y2 = y3;
		}
		this.m_i = null;
		j = null;
		if (A_0)
		{
			this.m_h = null;
		}
	}

	public override void Write(byte[] buffer, int offset, int count)
	{
		while (count > 0)
		{
			if (j.j == 0)
			{
				c();
			}
			_ = j.h;
			int num = j.a(buffer, offset, count);
			offset += num;
			count -= num;
			k += num;
			l += num;
		}
	}

	public void Write(bool value)
	{
		if (value)
		{
			Write((byte)1);
		}
		else
		{
			Write((byte)0);
		}
	}

	public void Write(byte value)
	{
		if (j.c(1))
		{
			j.g[j.h] = value;
		}
		else
		{
			c();
			j.g[j.h] = value;
		}
		j.b(1);
		l++;
		k++;
	}

	public unsafe void Write(short value)
	{
		if (!this.m_b)
		{
			value = global::j.a(value);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(short*)ptr = value;
		}
		Write(this.m_f, 0, 2);
	}

	public unsafe void Write(int value)
	{
		if (!this.m_b)
		{
			value = global::j.a(value);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(int*)ptr = value;
		}
		Write(this.m_f, 0, 4);
	}

	public unsafe void Write(long value)
	{
		if (!this.m_b)
		{
			value = global::j.a(value);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(long*)ptr = value;
		}
		Write(this.m_f, 0, 8);
	}

	public unsafe void Write(ushort value)
	{
		if (!this.m_b)
		{
			value = global::j.a(value);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(ushort*)ptr = value;
		}
		Write(this.m_f, 0, 2);
	}

	public unsafe void Write(uint value)
	{
		if (!this.m_b)
		{
			value = global::j.a(value);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(uint*)ptr = value;
		}
		Write(this.m_f, 0, 4);
	}

	public unsafe void Write(ulong value)
	{
		if (!this.m_b)
		{
			value = global::j.a(value);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(ulong*)ptr = value;
		}
		Write(this.m_f, 0, 8);
	}

	public void Write(DateTime value)
	{
		Write(value.Ticks);
	}

	public void Write(char value)
	{
		short value2 = (short)value;
		Write(value2);
	}

	public unsafe void Write(float value)
	{
		int value2 = *(int*)(&value);
		Write(value2);
	}

	public unsafe void Write(double value)
	{
		long value2 = *(long*)(&value);
		Write(value2);
	}

	public void Write(IMessage message)
	{
		message.Save(this);
	}

	public void Write<T>(IList<T> value) where T : IMessage
	{
		if (value != null && value.Count > 0)
		{
			Write(value.Count);
			for (int num = 0; num < value.Count; num++)
			{
				Write(value[num]);
			}
		}
		else
		{
			Write(0);
		}
	}

	public void WriteString(string value)
	{
		f().b(value, e, this);
	}

	public unsafe void WriteUTF(string value)
	{
		WriteBlock writeBlock = null;
		writeBlock = WriteResultBlock(this.m_f, 0, 2, this.m_g);
		ushort num = 0;
		if (!string.IsNullOrEmpty(value))
		{
			num = (ushort)f().b(value, e, this);
		}
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(ushort*)ptr = num;
		}
		writeBlock.SetData(this.m_f, 0, 2);
	}

	public unsafe void Write(string value)
	{
		WriteBlock writeBlock = null;
		writeBlock = WriteResultBlock(this.m_f, 0, 4, this.m_g);
		uint num = 0u;
		if (!string.IsNullOrEmpty(value))
		{
			num = (uint)f().b(value, e, this);
		}
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		fixed (byte* ptr = &this.m_f[0])
		{
			*(uint*)ptr = num;
		}
		writeBlock.SetData(this.m_f, 0, 4);
	}

	public void Write(ByteArraySegment sagment)
	{
		Write(sagment.Count);
		Write(sagment.Array, 0, sagment.Count);
	}

	[SpecialName]
	public bool h()
	{
		if (j.a(1) == 0)
		{
			return j.m == null;
		}
		return false;
	}

	public override int ReadByte()
	{
		if (j.a(1) == 0)
		{
			a(A_0: true);
		}
		byte result = j.g[j.h];
		j.d(1);
		l++;
		return result;
	}

	public override int Read(byte[] buffer, int offset, int count)
	{
		int num = 0;
		while (count > 0 && j != null)
		{
			int num2 = j.a(count);
			if (num2 > 0)
			{
				Buffer.BlockCopy(j.g, j.h, buffer, offset, num2);
				num += num2;
				offset += num2;
				count -= num2;
				j.d(num2);
				l += num2;
			}
			if (count > 0 && !a(A_0: false))
			{
				return num;
			}
		}
		return num;
	}

	public void Read(ByteArraySegment sagment)
	{
		int num = 0;
		num = ReadInt();
		num = Read(sagment.Array, 0, num);
		sagment.SetInfo(0, num);
	}

	public bool ReadBool()
	{
		if (ReadByte() != 0)
		{
			return true;
		}
		return false;
	}

	public short ReadShort()
	{
		Read(this.m_f, 0, 2);
		short num = BitConverter.ToInt16(this.m_f, 0);
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		return num;
	}

	public int ReadInt()
	{
		Read(this.m_f, 0, 4);
		int num = BitConverter.ToInt32(this.m_f, 0);
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		return num;
	}

	public long ReadLong()
	{
		Read(this.m_f, 0, 8);
		long num = BitConverter.ToInt64(this.m_f, 0);
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		return num;
	}

	public ushort ReadUShort()
	{
		Read(this.m_f, 0, 2);
		ushort num = BitConverter.ToUInt16(this.m_f, 0);
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		return num;
	}

	public uint ReadUInt()
	{
		Read(this.m_f, 0, 4);
		uint num = BitConverter.ToUInt32(this.m_f, 0);
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		return num;
	}

	public int g()
	{
		int num = 0;
		if (j != null)
		{
			num = j.i - j.h;
			for (y y2 = j.m; y2 != null; y2 = y2.m)
			{
				num += y2.i;
			}
		}
		return num;
	}

	public ulong ReadULong()
	{
		Read(this.m_f, 0, 8);
		ulong num = BitConverter.ToUInt64(this.m_f, 0);
		if (!this.m_b)
		{
			num = global::j.a(num);
		}
		return num;
	}

	public char ReadChar()
	{
		return (char)ReadShort();
	}

	public DateTime ReadDateTime()
	{
		return new DateTime(ReadLong());
	}

	public unsafe float ReadFloat()
	{
		int num = ReadInt();
		return *(float*)(&num);
	}

	public unsafe double ReadDouble()
	{
		long num = ReadLong();
		return *(double*)(&num);
	}

	public void Read(IMessage msg)
	{
		msg.Load(this);
	}

	public T ReadMessage<T>() where T : IMessage, new()
	{
		T val = new T();
		Read(val);
		return val;
	}

	public IList<T> ReadMessages<T>() where T : IMessage, new()
	{
		int num = ReadInt();
		List<T> list = new List<T>(num);
		for (int num2 = 0; num2 < num; num2++)
		{
			T item = new T();
			item.Load(this);
			list.Add(item);
		}
		return list;
	}

	public string ReadString(int length)
	{
		if (length <= 0)
		{
			return null;
		}
		return f().b(length, e, this);
	}

	public string ReadString()
	{
		int length = ReadInt();
		return ReadString(length);
	}

	public string ReadUTF()
	{
		ushort length = ReadUShort();
		return ReadString(length);
	}
}
